<template>
  <div>
    <h1>商品添加</h1>
    <el-form ref="form" label-width="80px" style="width: 500px">
      <el-form-item label="商品名称">
        <el-input v-model="good.name"></el-input>
      </el-form-item>
      <el-form-item label="商品描述">
        <el-input v-model="good.subtitle"></el-input>
      </el-form-item>
      <el-form-item label="所属分类">
        <!-- 一级品类 -->
        <el-select
          @change="a"
          v-model="classificationa"
          placeholder="请选择分类"
        >
          <el-option
            :label="item.name"
            :value="item.id"
            v-for="(item, index) in classificationlist"
            :key="index"
          ></el-option>
        </el-select>
        <!-- 二级品类 -->
        <el-select v-model="classificationb" placeholder="请选择分类">
          <el-option
            :label="item.name"
            :value="item.id"
            v-for="(item, index) in classificationlista"
            :key="index"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商品价格">
        <el-input v-model="good.price"
          ><template slot="append">元</template></el-input
        >
      </el-form-item>
      <el-form-item label="商品库存">
        <el-input v-model="good.stock"
          ><template slot="append">件</template></el-input
        >
      </el-form-item>
      <el-form-item label="图片上传">
        <el-upload
          action="/api/manage/product/upload.do"
          list-type="picture-card"
          :auto-upload="true"
          name="upload_file"
          :limit="1"
          :on-success="aa"
        >
          <i slot="default" class="el-icon-plus"></i>
          <div slot="file" slot-scope="{ file }">
            <img
              class="el-upload-list__item-thumbnail"
              :src="file.url"
              alt=""
            />
            <span class="el-upload-list__item-actions">
              <span
                class="el-upload-list__item-preview"
                @click="handlePictureCardPreview(file)"
              >
                <i class="el-icon-zoom-in"></i>
              </span>
              <span
                v-if="!disabled"
                class="el-upload-list__item-delete"
                @click="handleDownload(file)"
              >
                <i class="el-icon-download"></i>
              </span>
              <span
                v-if="!disabled"
                class="el-upload-list__item-delete"
                @click="handleRemove(file)"
              >
                <i class="el-icon-delete"></i>
              </span>
            </span>
          </div>
        </el-upload>
        <el-dialog :visible.sync="dialogVisible">
          <img width="100%" :src="dialogImageUrl" alt="" />
        </el-dialog>
      </el-form-item>
      <el-form-item label="富文本" class="from-item">
        <quill-editor
          :options="editorOption"
          @change="onEditorChange($event)"
        />
      </el-form-item>

      <el-button type="success" @click="add">添加</el-button>

    </el-form>
  </div>
</template>

<script>
import { add, classification } from "../../../http/http";
export default {
  name: "",
  components: {},
  props: [],
  data() {
    return {
      good: {
        imageHost: "",
        mainImage: "",
        name: "",
        subtitle: "",
        price: "",
        stock: "",
        status: "1",
        subImages: "",
        detail: "",
      },
      classificationa: "",
      classificationb: "",
      classificationlist: [],
      classificationlista: [],
      // 上传图片
      dialogImageUrl: "",
      dialogVisible: false,
      disabled: false,
      // 富文本
      editorOption: {
        // Some Quill options...
      },
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {
    this.classification();
  },
  methods: {
    // 一级品类
    async classification() {
      const { data: res } = await classification();
      this.classificationlist = res.data;
    },
    // 二级菜单点击只后获取二级品类
    async a(i) {
      const { data: res } = await classification(i);
      this.classificationlista = res.data;
    },
    // 添加产品
    async add() {
      const { data: res } = await add(this.good);
      this.$message.success("添加成功");
    },
    // 图片上传
    handleRemove(file) {
      console.log(file);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    handleDownload(file) {
      console.log(file);
    },
    // 上传成功后
    aa(response, file, fileList) {
      this.good.subImages = file.response.data.uri;
    },
    // 富文本
    onEditorChange({ quill, html, text }) {
      console.log("editor change!", quill, html, text);
      console.log(text)
      this.good.detail = text;
    },
  },
};
</script>

<style lang='scss' scoped>
.from-item{
  height: 300px;
}
.quill-editor{
  width: 900px;
  height: 200px;
}
</style>
